package com.hsav.kcxb.fragment.sing;

import android.app.Fragment;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.Toast;

import com.hsav.kcxb.R;
import com.hsav.kcxb.view.ToneAdjuster;

/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link AdjustVolumeFragment.OnFragmentInteractionListener} interface
 * to handle interaction events.
 * Use the {@link AdjustVolumeFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class AdjustVolumeFragment extends Fragment implements SeekBar.OnSeekBarChangeListener, View.OnClickListener {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param_tone";
    private static final String ARG_PARAM2 = "param_volume";

    private OnFragmentInteractionListener mListener;
    private ToneAdjuster toneAdjuster;
    private int tone;
    private int volume;

    public AdjustVolumeFragment() {
        // Required empty public constructor
    }

    /**
     *
     * @param tone 音调
     * @param volume 音量
     * @return
     */
    // TODO: Rename and change types and number of parameters
    public static AdjustVolumeFragment newInstance(int tone, int volume) {
        AdjustVolumeFragment fragment = new AdjustVolumeFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_PARAM1, tone);
        args.putInt(ARG_PARAM2, volume);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            tone = getArguments().getInt(ARG_PARAM1);
            volume = getArguments().getInt(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_adjust_volume, container, false);
        FrameLayout frameRoot = (FrameLayout) rootView.findViewById(R.id.frame_root);
        frameRoot.setOnClickListener(this);
        SeekBar barVolume = (SeekBar) rootView.findViewById(R.id.bar_volume);
        barVolume.setOnSeekBarChangeListener(this);
        barVolume.setProgress(volume);
        ImageButton btnUp = (ImageButton) rootView.findViewById(R.id.btn_up);
        btnUp.setOnClickListener(this);
        ImageButton btnDown = (ImageButton) rootView.findViewById(R.id.btn_down);
        btnDown.setOnClickListener(this);
        toneAdjuster = (ToneAdjuster) rootView.findViewById(R.id.tone_adjuster);
        toneAdjuster.setSelectedTone(tone);
        return rootView;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        if (toneAdjuster == null) return;
        Toast.makeText(getActivity(), String.format("当前音量：%s", seekBar.getProgress()), Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.btn_up:
                toneAdjuster.up();
                break;
            case R.id.btn_down:
                toneAdjuster.down();
                break;
            case R.id.frame_root:
                if (mListener != null)
                    mListener.onRequestClose(this);
                break;
        }
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnFragmentInteractionListener {
        void onRequestClose(Fragment sender);
    }
}
